package com.xiangyi.teachingsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiangyi.teachingsystem.entity.po.TeacherReview;
import com.xiangyi.teachingsystem.entity.vo.Result;
import com.xiangyi.teachingsystem.service.TeacherReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TeacherReviewController {
    @Autowired
    TeacherReviewService teacherReviewService;

    @PostMapping("/addOrUpdateTeaReview")
    public Result addOrUpdateTeaReview(@RequestBody TeacherReview teacherReview){
        Integer groupId = teacherReview.getGroupId();
        Integer taskId = teacherReview.getTaskId();
        TeacherReview cherk = teacherReviewService.getOne(new QueryWrapper<TeacherReview>()
                .eq("group_id", groupId)
                .eq("task_id", taskId));
        if (cherk==null){
            try {
                teacherReviewService.save(teacherReview);
                return Result.success("添加成功");
            }catch (Exception e){
                e.printStackTrace();
                return Result.failure("服务器错误").setData(e.getMessage());
            }

        }else {
            try {
                teacherReviewService.update(teacherReview,new QueryWrapper<TeacherReview>()
                        .eq("group_id", groupId)
                        .eq("task_id", taskId));
                return Result.success("修改成功");
            }catch (Exception e){
                e.printStackTrace();
                return Result.failure("服务器错误").setData(e.getMessage());
            }

        }
    }
}
